<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/22
 * Time: 10:56
 */

namespace app\common\model;


use app\common\Enum\CommonEnum;
use think\facade\Request;
use think\Model;

class QqCodeMapping extends Model
{
    protected $table = 'voq_qq_code_mapping';
    public $autoWriteTimestamp = 'datetime';
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $auto = ['create_ip','pay_status'];
    protected $append = ['codeByStatus'];

    public function getStatusAttr($value,$data)
    {
        if ($value == CommonEnum::COMMON_VALID){
            if ((isset($data['end_time']) && time()<strtotime($data['end_time'])) || !$data['end_time']) {
                return true;
            }
        }
        return false;
    }

    public function getCodeAttr($value,$data)
    {
        $strs = str_split($value,4);
        return implode($strs,'-');
    }

    public function setCreateIpAttr($value,$data)
    {
        return Request::ip();
    }

    public function setPayStatusAttr($value,$data)
    {
        return $value?:CommonEnum::COMMON_INVALID;
    }

    public function getCodeByStatusAttr($value,$data)
    {
        if ($data['status'] == CommonEnum::COMMON_VALID && $data['activation_state'] == CommonEnum::COMMON_VALID){
            return $this->getCodeAttr($data['code'],null);
        }
        return false;
    }
}